home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / MacGzip 0.1b2 Source / gzip-1.2.4 sources / macos / think / MacGzip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-13  |  7.3 KB  |  384 lines  |  [TEXT/KAHL]

  1. /*
  2.  * Copyright (C) 1993  SPDsoft
  3.  * 
  4.  */
  5.  
  6. #include <GestaltEqu.h>
  7. #include <Sound.h>
  8.  
  9. #include "MacGzip.h"
  10. #include "thePrefs.h"
  11. #include "MacErrors.h"
  12. #include "SPDProg.h"
  13.  
  14. #include <string.h>
  15.  
  16. void about(void);
  17. void InitTheMac(void);
  18. int OpenFromFinder( void );
  19. void MyBeep(void);
  20.  
  21. static pascal char  MyFilter ( DialogPtr theDialog, EventRecord *theEvent,short  *itemHit );
  22.  
  23. extern OSErr DoOpen( Str255 fName, long vRefNum);
  24.  
  25. /* CONST */
  26. #define NIL 0L
  27.  
  28. static SFReply reply;
  29. SFTypeList typeList={'Gzip','ZIVU','pZIP','ZIVM'};
  30. static Point myPoint={80,80};
  31.  
  32. Boolean        quitting = false;
  33.  
  34. MenuHandle    myMenus[4];
  35. OSErr theOSErr;
  36. KeyMap    theKeys;
  37.  
  38.  
  39. void InitTheMac(void)
  40. {
  41. long gestAliasResponse, gestStdFileResponse, gestFSResponse;
  42.  
  43.     FlushEvents(everyEvent, 0);
  44.     InitGraf(&qd.thePort);
  45.     InitFonts();
  46.     InitWindows();
  47.     InitMenus();
  48.     TEInit();
  49.     InitDialogs(NIL);
  50.     InitCursor();
  51.     MaxApplZone();
  52.  
  53.     GetKeys(&theKeys);
  54.  
  55. /* sys 7 ?*/
  56.  
  57.     theOSErr = Gestalt(gestaltAliasMgrAttr, &gestAliasResponse);
  58.     if (theOSErr == noErr) theOSErr = Gestalt(gestaltFSAttr, &gestFSResponse);
  59.     if (theOSErr == noErr)
  60.         theOSErr = Gestalt(gestaltStandardFileAttr, &gestStdFileResponse);
  61.     
  62.     if (theOSErr != noErr ||
  63.         (gestAliasResponse & (1 << gestaltAliasMgrPresent)) == 0 ||
  64.         (gestFSResponse & (1 << gestaltHasFSSpecCalls)) == 0 ||
  65.         (gestStdFileResponse & (1 << gestaltStandardFile58)) == 0) {
  66.     
  67.         theAlert( OLD_SYSTEM,GENERIC, theOSErr, true );
  68.     }
  69.     
  70.     CanBreak=true;
  71.  
  72.     if (!GetOrGeneratePrefs(&currPrefs, kCurrentPrefsTypeVers))
  73.     {
  74.         about();
  75.         prefsChangedFlag = DoPrefsDialog(&currPrefs);
  76.         if (prefsChangedFlag)
  77.         {
  78.             if( ( theOSErr = SavePrefs(&currPrefs)) != noErr )
  79.                     theAlert(NO_SAVE_PREF,GENERIC,theOSErr, true );
  80.         }
  81.         else ExitToShell();
  82.     }
  83.  
  84. }
  85.  
  86. main()
  87.  
  88. { /* main program */
  89.     extern    Boolean        gSavedPos;
  90.     extern    Point        gSavedPoint;
  91.  
  92.     InitTheMac();
  93.     SetUpMenus();
  94.     
  95.     if ( 0!=OpenFromFinder() )
  96.     {
  97.         quitting=true;
  98.     }
  99.     
  100.     while( !quitting )
  101.     {
  102.         MainEvent();
  103.     }
  104.  
  105. /*    ReleaseAnimatedCursors();*/
  106. /* remember window pos */
  107.             
  108.     currPrefs.SavedPos = gSavedPos; /* always true */
  109.         
  110.     if(( gSavedPoint.h != currPrefs.SavedPoint.h ) ||
  111.         ( gSavedPoint.v != currPrefs.SavedPoint.v ) )
  112.     {
  113.         currPrefs.SavedPoint.h = gSavedPoint.h;
  114.         currPrefs.SavedPoint.v = gSavedPoint.v;
  115.         prefsChangedFlag=true;
  116.     }
  117.  
  118.     if (prefsChangedFlag)
  119.     {
  120.         if( ( theOSErr = SavePrefs(&currPrefs)) != noErr )
  121.                 theAlert(NO_SAVE_PREF,GENERIC,theOSErr, true );
  122.     }
  123.     
  124.     
  125.     ExitToShell();
  126. }
  127.  
  128.  
  129. void MainEvent(void)
  130. {
  131.     EventRecord myEvent;
  132.     WindowPtr whichWindow;
  133.     short windowPart;
  134.  
  135.     SystemTask();
  136.     if( GetNextEvent(everyEvent, &myEvent) ) {
  137.         switch( myEvent.what ) {
  138.             case mouseDown:
  139.                 windowPart = FindWindow(myEvent.where, &whichWindow);
  140.                 DoMouseDown(windowPart, whichWindow, &myEvent);
  141.                 break;
  142.                 
  143.             case keyDown:
  144.             case autoKey: 
  145.             {
  146.                 register char theChar;
  147.     
  148.                 theChar = myEvent.message & charCodeMask;
  149.                 if ((myEvent.modifiers & cmdKey) != 0) 
  150.                     DoCommand( MenuKey(theChar) );
  151.                                     
  152.                 break;
  153.             }
  154.                 
  155.             case activateEvt:
  156.                 break;
  157.  
  158.             case updateEvt: 
  159.                 break;
  160.         }
  161.     }
  162. }
  163. void DoCommand(long mResult)
  164. {
  165.     short    theItem;
  166.     Str255    name;
  167.  
  168.     theItem = LoWord(mResult);
  169.     switch( HiWord(mResult) ) {
  170.         case appleID:
  171.             if( theItem > 2 ) {
  172.                 CGrafPtr savePort;
  173.                 GetItem(myMenus[appleM], theItem, &name);
  174.                 GetPort(&savePort);
  175.                 OpenDeskAcc(name);
  176.                 SetPort(savePort);
  177.             }
  178.             else
  179.             {
  180.                 about();
  181.             }
  182.             break;
  183.  
  184.         case fileID:
  185.             DoFile(theItem);
  186.             break;
  187.  
  188.         case editID:
  189.             SystemEdit(theItem-1);
  190.             break;
  191.             
  192.         case gzipID:
  193.         
  194.             switch(theItem){
  195.             
  196.             case gmAscii:
  197.             
  198.                 currPrefs.ascii=true;
  199.                 CheckItem( myMenus[gzipM], gmBin, false );
  200.                 CheckItem( myMenus[gzipM], gmAscii, true);
  201.                 break;
  202.  
  203.             case gmBin:
  204.             
  205.                 currPrefs.ascii=false;
  206.                 CheckItem( myMenus[gzipM], gmAscii , false );
  207.                 CheckItem( myMenus[gzipM], gmBin, true);
  208.                 break;
  209.             
  210.             case gmComp:
  211.             
  212.                 currPrefs.compress=true;
  213.                 CheckItem( myMenus[gzipM], gmUncomp, false );
  214.                 CheckItem( myMenus[gzipM], gmComp, true);
  215.                 break;
  216.  
  217.             case gmUncomp:
  218.             
  219.                 currPrefs.compress=false;
  220.                 CheckItem( myMenus[gzipM], gmComp , false );
  221.                 CheckItem( myMenus[gzipM], gmUncomp, true);
  222.                 break;
  223.             
  224.             }
  225.             prefsChangedFlag = true;
  226.             break;
  227.     }
  228.  
  229.     HiliteMenu(0);
  230. }
  231.  
  232. void DoFile(short item)
  233. {
  234.     switch( item ) {
  235.         case fmOpen:
  236.         
  237.             SFGetFile(
  238.                         myPoint,
  239.                         "\p",
  240.                         0L,
  241.                         -1,
  242. /*                        currPrefs.compress ? -1:4,*/
  243.                         typeList,
  244.                         0L,
  245.                         &reply
  246.                     );
  247.             
  248.             if( reply.good != false )
  249.             {
  250.                 if( DoOpen( reply.fName, reply.vRefNum )!=128)
  251.                 {
  252.                     if(currPrefs.BeepWhenDone) MyBeep();
  253.                     if(currPrefs.QuitWhenDone) quitting=true;
  254.                 }
  255.             }
  256.             break;
  257.  
  258.         case fmPrefs:
  259.         
  260.             prefsChangedFlag = DoPrefsDialog(&currPrefs);
  261.             if (prefsChangedFlag)
  262.             {
  263.                 if( ( theOSErr = SavePrefs(&currPrefs)) != noErr )
  264.                         theAlert(NO_SAVE_PREF,GENERIC,theOSErr, true );
  265.             }
  266.             break;
  267.  
  268.         case fmQuit:
  269.         
  270.             quitting = true;
  271.             break;
  272.     }
  273. }
  274. void DoMouseDown(short windowPart, WindowPtr whichWindow, EventRecord *myEvent)
  275. {
  276.     switch( windowPart ) {
  277.         case inGoAway:
  278.             break;
  279.  
  280.         case inMenuBar:
  281.             DoCommand( MenuSelect(myEvent->where) );
  282.             break;
  283.  
  284.         case inSysWindow:
  285.             SystemClick(myEvent, whichWindow);
  286.             break;
  287.  
  288.         case inDrag:
  289.             break;
  290.  
  291.         case inGrow:
  292.             break;
  293.  
  294.         case inContent:
  295.             if( whichWindow != FrontWindow() )
  296.                 SelectWindow(whichWindow);
  297.             break;
  298.     }
  299. }
  300.  
  301. int OpenFromFinder( void )
  302. {
  303.     int    message, count, index;
  304.     AppFile        theFile;
  305.     Boolean PrefCSave,PrefASave;
  306.     
  307.     PrefCSave =    currPrefs.compress;
  308.     currPrefs.compress = (( IsOptKey(theKeys) ) ? true : false);
  309.     PrefASave =    currPrefs.ascii;
  310.     if (IsAKey(theKeys)) currPrefs.ascii = true;
  311.     else if (IsBKey(theKeys)) currPrefs.ascii = false;
  312.     
  313.     CountAppFiles( &message, &count);            /*any files to open?*/
  314.     for( index= 1 ; index <= count ; index ++)
  315.     {
  316.         GetAppFiles(index, &theFile);
  317.         
  318.         if( DoOpen(theFile.fName, theFile.vRefNum)!=128)
  319.         {
  320.             if(currPrefs.BeepWhenDone) MyBeep();
  321.         }
  322.     }
  323.     
  324.     ClrAppFiles(count);
  325.  
  326.     currPrefs.compress=PrefCSave;
  327.     currPrefs.ascii = PrefASave;
  328.  
  329.     return count;
  330. }
  331.  
  332. /***************************************************************************/
  333. void   about( void )
  334. {
  335. DialogPtr    GetSelection;              
  336. short    itemHit;                       
  337.  
  338.     GetSelection = GetNewDialog(dlogAbout, NIL, (WindowPtr)-1);
  339.     ShowWindow(GetSelection);           
  340.     SelectWindow(GetSelection);         
  341.     SetPort(GetSelection);              
  342.     ModalDialog(MyFilter, &itemHit);
  343.     DisposDialog(GetSelection);         
  344. }                                       
  345. static pascal char  MyFilter (
  346. DialogPtr   theDialog,      
  347. EventRecord   *theEvent,    
  348. short  *itemHit
  349. )
  350. {
  351.     return ((theEvent->what == keyDown)||(theEvent->what == mouseDown));
  352. }
  353. /***************************************************************************/
  354. void SetUpMenus(void)
  355. {
  356.     short index;
  357.     
  358.     ClearMenuBar();
  359.     
  360.     myMenus[appleM] = GetMenu(appleID);
  361.     AddResMenu(myMenus[appleM], 'DRVR');
  362.     myMenus[fileM] = GetMenu(fileID);
  363.     myMenus[editM] = GetMenu(editID);
  364.     myMenus[gzipM] = GetMenu(gzipID);
  365.     for(index=appleM; index <= gzipM; index++)
  366.         InsertMenu(myMenus[index], 0) ;
  367.  
  368.     CheckItem( myMenus[gzipM], (currPrefs.ascii?gmAscii:gmBin), true);
  369.     CheckItem( myMenus[gzipM], (currPrefs.compress?gmComp:gmUncomp), true);
  370.     
  371.     DrawMenuBar();
  372. }
  373. /***************************************************************************/
  374. void MyBeep(void)
  375. {
  376.     Handle theSound;
  377.     
  378.         theSound = GetResource('snd ', SOUND_ID);
  379.             if (theSound != nil)
  380.             {
  381.                 SndPlay(nil, theSound, false);
  382.                 ReleaseResource(theSound);
  383.             }
  384. }